package com.htht.job.executor.service.treenode;

import com.htht.job.core.api.treenode.TreeNodeService;
import com.htht.job.core.biz.model.ReturnT;
import com.htht.job.executor.dao.tree.TreeNodeDao;
import com.htht.job.executor.model.tree.TreeNode;
import com.htht.job.executor.redis.RedisKeys;
import com.htht.job.executor.redis.RedisService;
import com.htht.job.executor.util.specification.SimpleSpecificationBuilder;
import org.apache.commons.lang3.StringUtils;
import org.jeesys.common.jpa.dao.string.BaseDao;
import org.jeesys.common.jpa.search.Specifications;
import org.jeesys.common.jpa.search.Specifications.Builder;
import org.jeesys.common.jpa.service.string.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service
public class TreeNodeServiceImpl extends BaseService<TreeNode> implements
		TreeNodeService {
	@Autowired
	private TreeNodeDao treeNodeDao;
	@Autowired
	private RedisService redisService;

	@Override
	public BaseDao<TreeNode> getBaseDao() {
		// TODO Auto-generated method stub
		return treeNodeDao;
	}

	@Cacheable(value = "treeNode")
	public List<TreeNode> getTreeNodeById() {
		/** =======1=====获取list============ **/
		Sort sort = new Sort(Sort.Direction.ASC, "createTime");

		List<TreeNode> treeNodes = treeNodeDao.findAll(sort);
		/** =======2=====递归获取子节点========== **/
		List<TreeNode> trees = new ArrayList<TreeNode>();
		for (TreeNode treeNode : treeNodes) {
			if ("0".equals(treeNode.getParentId())) {
				trees.add(getNodes(treeNode, treeNodes));
			}
		}
		return trees;
	}

	private TreeNode getNodes(TreeNode treeNode, List<TreeNode> treeNodes) {
		for (TreeNode it : treeNodes) {
			if (treeNode.getId().equals(it.getParentId())) {
				if (treeNode.getNodes() == null) {
					treeNode.setNodes(new ArrayList<TreeNode>());
				}
				treeNode.getNodes().add(getNodes(it, treeNodes));
			}

		}

		return treeNode;
	}
	
	/**分页查询产品
	 * @param start
	 * @param length
	 * @param treeNode
	 * @return
	 */
	public Map<String, Object> pageList(int start, int length,
			TreeNode treeNode) {
		Page<TreeNode> page = this.findPageTreeNode(start, length,
				treeNode);
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("recordsTotal", page.getTotalElements()); // 总记录数
		maps.put("recordsFiltered", page.getTotalElements()); // 过滤后的总记录数
		maps.put("data", page.getContent()); // 分页列表
		return maps;
	}
	
	
	public Page<TreeNode> findPageTreeNode(int start, int length,
										   TreeNode treeNode){
			Sort sort = new Sort(Sort.Direction.DESC, "createTime");
			PageRequest d = new PageRequest(start, length, sort);
			Page<TreeNode> page = this.getPage(
					getWhereClause(treeNode), d);
		 
			return page;
	}
	private Specification<TreeNode> getWhereClause(
			TreeNode treeNode) {
		SimpleSpecificationBuilder<TreeNode> specification=new SimpleSpecificationBuilder();
		if (!StringUtils.isEmpty(treeNode.getText())) {
			specification.add("text","likeAll",treeNode.getText());
		}
		if (!StringUtils.isEmpty(treeNode.getMenuId())) {
			specification.add("menuId","likeAll",treeNode.getMenuId());
		}
		if (!StringUtils.isEmpty(treeNode.getId())) {
			specification.addOr("id","eq",treeNode.getId());

		}

		return specification.generateSpecification();
	}
	@CacheEvict(value = "treeNode")
	//@CachePut(value = "treeNode",key = "#root.caches[0].name + ':' + #treeNode.id")
	public TreeNode saveTreeNode(TreeNode treeNode){
		TreeNode newTreeNode =new TreeNode();
		if(!StringUtils.isEmpty(treeNode.getId())){
			TreeNode yTreeNode =this.getById(treeNode.getId());
			//yTreeNode.setGdbpath(treeNode.getGdbpath());
			//yTreeNode.setStoragepath(treeNode.getStoragepath());
			yTreeNode.setMenu(treeNode.getMenu());
			yTreeNode.setMenuId(treeNode.getMenuId());
			yTreeNode.setUpdateTime(new Date());
			yTreeNode.setText(treeNode.getText());
			newTreeNode =this.save(yTreeNode);

		}else{
			if(StringUtils.isEmpty(treeNode.getParentId())){
				treeNode.setParentId("0");
			}
			treeNode.setCreateTime(new Date());
			newTreeNode =this.save(treeNode);
		}
		return newTreeNode;
	}
	@CacheEvict(value = "treeNode")
	public ReturnT<String> deleteTreeNode(String id) {
		try {
			this.delete(id);
			return ReturnT.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ReturnT.FAIL;
	}

}
